package borderPatrol.Game;

import java.util.Scanner;

import borderPatrol.Data.Board;
import borderPatrol.Data.Move;
import borderPatrol.Players.CFRPlayer;
import borderPatrol.Players.HumanPlayer;
import borderPatrol.Players.MCTSPlayer;
import borderPatrol.Players.OverconfidentPlayer;
import borderPatrol.Players.ParanoidPlayer;
import borderPatrol.Players.Player;

public class GameRunner {
	
	private final int WIDTH = 3;
	private final int HEIGTH = 3;
	
	private Board board;
	
	private static Player poacherPlayer = null;
	private static Player patrollerPlayer = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		String playerOne;
		String playerTwo;
		

		System.out.println("******************************************");
		System.out.println("Welcome in game Border Patrol");
		System.out.println("******************************************");

		do {
			System.out.println("Please choose player one from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerOne = scanner.next();

			if (playerOne.equals("1")) {
				poacherPlayer = new MCTSPlayer(1);
			} else if (playerOne.equals("2")) {
				poacherPlayer = new CFRPlayer(1);
			} else if (playerOne.equals("3")) {
				poacherPlayer = new ParanoidPlayer(1);
			} else if (playerOne.equals("4")) {
				poacherPlayer = new OverconfidentPlayer(1);
			} else if (playerOne.equals("5")) {
				poacherPlayer = new HumanPlayer(1);
			}
		} while (poacherPlayer == null);
		
		do {
			System.out.println("Please choose player two from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerTwo = scanner.next();

			if (playerTwo.equals("1")) {
				patrollerPlayer = new MCTSPlayer(2);
			} else if (playerTwo.equals("2")) {
				patrollerPlayer = new CFRPlayer(2);
			} else if (playerTwo.equals("3")) {
				patrollerPlayer = new ParanoidPlayer(2);
			} else if (playerTwo.equals("4")) {
				patrollerPlayer = new OverconfidentPlayer(2);
			} else if (playerTwo.equals("5")) {
				patrollerPlayer = new HumanPlayer(2);
			}
		} while (patrollerPlayer == null);
		
		new GameRunner().runGame();

	}
	
	private void runGame() {

		board = new Board(WIDTH, HEIGTH);

		while (board.isTerminal() == 0) {

			System.out.println("id: " + board.getId());

			System.out.println("poacher's position: " + board.getPoacherPosition()[0] + " " + board.getPoacherPosition()[1]);
			System.out.println("patroller's position: " + board.getPatrollerPosition()[0] + " " + board.getPatrollerPosition()[1]);
			
			System.out.println("Available actions for poacher: " + board.getActionsForPlayer(1));
			Move poacherMove = poacherPlayer.getNextMove(board.getPoacherBoard());
			System.out.println("Available actions for patroller: " + board.getActionsForPlayer(2));
			Move patrollerMove = patrollerPlayer.getNextMove(board.getPatrollerBoard());

			board.makeMove(poacherMove, 1);
			board.makeMove(patrollerMove, 2);

			board.endTurn();

		}
		
		 System.out.println();
		 System.out.println("*************************");
		if (board.isTerminal() == -1) {
			System.out.println("It's a tie.");
		} else {
			System.out.println("Player " + board.isTerminal() + " won.");
		}
		 System.out.println("*************************");
	}
}
